package CCRMClient.GUI;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import CCRMClient.ClientStart;
import Common.Entities.STATUS;
import Common.Entities.Type;

import java.awt.Color;
import java.awt.Image;

import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.SystemColor;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ViewProductScreen extends JPanel {
	
	private long productID;
	private String productName;
	private String productDescription;
	private float productBasePrice;
	private STATUS productStatus;
	private LinkedList<Type> productCategoriesAndTypes;
	private File picFile;
	private String pictureTitle;
	private Image productPicture;
	
	private JLabel lblProductDetails;
	private JLabel lblProductName;
	private JTextField textProductName;
	private JLabel lblProductDescription;
	private JTextArea textAreaProductDescription;
	private JLabel lblBasePrice;
	private JTextField textBasePrice;
	private JLabel lblProductStatus;
	private JTextField textProductStatus;
	private JLabel lblPictureTitle;
	private JTextField textPictureTitle;
	private JLabel lblPicture;
	private JScrollPane scpTypes;
	private JTable tblTypes;
	private JButton btnGoBackEP;
	private JLabel lblProductId;
	private JTextField textProductID;
	private JButton btnBackToSearch;
	private JButton btnBackToTemplate;

	/**
	 * Create the panel.
	 */
	public ViewProductScreen() {

		initialize();
	}
	private void initialize() {
		setSize (987,775);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblProductDetails());
		add(getLblProductId());
		add(getTextProductID());
		add(getLblProductName());
		add(getTextProductName());
		add(getLblProductDescription());
		add(getTextAreaProductDescription());
		add(getLblBasePrice());
		add(getTextBasePrice());
		add(getLblProductStatus());
		add(getTextProductStatus());
		add(getLblPictureTitle());
		add(getTextPictureTitle());
		add(lblPicture());
		add(getScpTypes());
		add(getBtnGoBackEP());
		add(getBtnBackToSearch());
		add(getBtnBackToTemplate());
	}

	/**
	 * @param productID
	 * @param productName
	 * @param productDescription
	 * @param productBasePrice
	 * @param productStatus
	 * @param productCategoriesAndTypes
	 * @param pictureTitle
	 * @param picFile
	 */
	public ViewProductScreen(long productID, String productName,
			String productDescription, float productBasePrice,
			STATUS productStatus, LinkedList<Type> productCategoriesAndTypes,
			String pictureTitle, File picFile) {
		this();
		this.productID = productID;
		this.productName = productName;
		this.productDescription = productDescription;
		this.productBasePrice = productBasePrice;
		this.productStatus = productStatus;
		this.productCategoriesAndTypes = productCategoriesAndTypes;
		this.pictureTitle = pictureTitle;
		this.picFile = picFile;
		loadProductToScreen();
	}
	
	/**
	 * This method load product data and picture into the screen
	 */
	private void loadProductToScreen() {
		// load product data
		textProductID.setText(new Long(productID).toString());
		textProductID.setEnabled(true);
		textProductID.setEditable(false);
		textProductName.setText(productName);
		textProductName.setEnabled(true);
		textProductName.setEditable(false);
		textAreaProductDescription.setText(productDescription);
		textAreaProductDescription.setEnabled(true);
		textAreaProductDescription.setEditable(false);
		textBasePrice.setText(new Float(productBasePrice).toString());
		textBasePrice.setEnabled(true);
		textBasePrice.setEditable(false);
		switch (productStatus) {
		case ACTIVE:
			textProductStatus.setText("Active");
			break;
		case NOT_ACTIVE:
			textProductStatus.setText("Not Active");
			break;
		default:
			textProductStatus.setText("Suspended");
		}
		textProductStatus.setEnabled(true);
		textProductStatus.setEditable(false);
		// load picture
		if (pictureTitle != null) {
			textPictureTitle.setText(pictureTitle);
			textPictureTitle.setEnabled(true);
			textPictureTitle.setEditable(false);
		}
		if (picFile != null) {
			BufferedImage bufPic = null;
			try {
				bufPic = ImageIO.read(picFile);
				Image fitPic = bufPic.getScaledInstance(lblPicture.getWidth(), lblPicture.getHeight(), Image.SCALE_SMOOTH);
				productPicture = new ImageIcon (fitPic).getImage();
				lblPicture.setIcon(new ImageIcon(productPicture));
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Problem loading product picture", "View Product", JOptionPane.ERROR_MESSAGE);
			}
		} else
			JOptionPane.showMessageDialog(null, "This product has no picture", "View Product", JOptionPane.INFORMATION_MESSAGE);
		// load categories and types
		if (!productCategoriesAndTypes.isEmpty()) {
			DefaultTableModel dtm = (DefaultTableModel) tblTypes.getModel();
			Object[] tblRow = new Object[2];
			Type typ = null;
			for (int i = 0 ; i < productCategoriesAndTypes.size() ; i ++) {
				typ = productCategoriesAndTypes.get(i);
				tblRow[0] = typ.getCategoryName();
				tblRow[1] = typ.getItmName();
				dtm.addRow(tblRow);
			}
		}
	}
	
	private JLabel getLblProductDetails() {
		if (lblProductDetails == null) {
			lblProductDetails = new JLabel("Product Details");
			lblProductDetails.setHorizontalAlignment(SwingConstants.CENTER);
			lblProductDetails.setForeground(Color.RED);
			lblProductDetails.setFont(new Font("David", Font.BOLD, 26));
			lblProductDetails.setBackground(Color.WHITE);
			lblProductDetails.setBounds(0, 0, 987, 49);
		}
		return lblProductDetails;
	}
	private JLabel getLblProductName() {
		if (lblProductName == null) {
			lblProductName = new JLabel("Product Name:");
			lblProductName.setFont(new Font("David", Font.BOLD, 20));
			lblProductName.setBounds(21, 93, 164, 29);
		}
		return lblProductName;
	}
	private JTextField getTextProductName() {
		if (textProductName == null) {
			textProductName = new JTextField();
			textProductName.setEnabled(false);
			textProductName.setEditable(false);
			textProductName.setColumns(10);
			textProductName.setBackground(SystemColor.menu);
			textProductName.setBounds(246, 98, 251, 20);
		}
		return textProductName;
	}
	private JLabel getLblProductDescription() {
		if (lblProductDescription == null) {
			lblProductDescription = new JLabel("Product Description:");
			lblProductDescription.setFont(new Font("David", Font.BOLD, 20));
			lblProductDescription.setBounds(20, 133, 205, 29);
		}
		return lblProductDescription;
	}
	private JTextArea getTextAreaProductDescription() {
		if (textAreaProductDescription == null) {
			textAreaProductDescription = new JTextArea();
			textAreaProductDescription.setEnabled(false);
			textAreaProductDescription.setEditable(false);
			textAreaProductDescription.setLineWrap(true);
			textAreaProductDescription.setFont(new Font("Courier New", Font.PLAIN, 11));
			textAreaProductDescription.setBorder(new LineBorder(new Color(0, 0, 0)));
			textAreaProductDescription.setBackground(SystemColor.menu);
			textAreaProductDescription.setBounds(85, 173, 412, 109);
		}
		return textAreaProductDescription;
	}
	private JLabel getLblBasePrice() {
		if (lblBasePrice == null) {
			lblBasePrice = new JLabel("Product's Base Price:");
			lblBasePrice.setFont(new Font("David", Font.BOLD, 20));
			lblBasePrice.setBounds(20, 297, 205, 29);
		}
		return lblBasePrice;
	}
	private JTextField getTextBasePrice() {
		if (textBasePrice == null) {
			textBasePrice = new JTextField();
			textBasePrice.setEnabled(false);
			textBasePrice.setEditable(false);
			textBasePrice.setColumns(10);
			textBasePrice.setBackground(SystemColor.menu);
			textBasePrice.setBounds(246, 302, 251, 20);
		}
		return textBasePrice;
	}
	private JLabel getLblProductStatus() {
		if (lblProductStatus == null) {
			lblProductStatus = new JLabel("Product Status:");
			lblProductStatus.setFont(new Font("David", Font.BOLD, 20));
			lblProductStatus.setBounds(20, 337, 205, 29);
		}
		return lblProductStatus;
	}
	private JTextField getTextProductStatus() {
		if (textProductStatus == null) {
			textProductStatus = new JTextField();
			textProductStatus.setEnabled(false);
			textProductStatus.setEditable(false);
			textProductStatus.setColumns(10);
			textProductStatus.setBackground(SystemColor.menu);
			textProductStatus.setBounds(246, 342, 251, 20);
		}
		return textProductStatus;
	}
	private JLabel getLblPictureTitle() {
		if (lblPictureTitle == null) {
			lblPictureTitle = new JLabel("Picture's Title:");
			lblPictureTitle.setFont(new Font("David", Font.BOLD, 20));
			lblPictureTitle.setBounds(526, 62, 164, 29);
		}
		return lblPictureTitle;
	}
	private JTextField getTextPictureTitle() {
		if (textPictureTitle == null) {
			textPictureTitle = new JTextField();
			textPictureTitle.setEnabled(false);
			textPictureTitle.setEditable(false);
			textPictureTitle.setColumns(10);
			textPictureTitle.setBackground(SystemColor.menu);
			textPictureTitle.setBounds(700, 67, 251, 20);
		}
		return textPictureTitle;
	}
	private JLabel lblPicture() {
		if (lblPicture == null) {
			lblPicture = new JLabel("");
			lblPicture.setBounds(651, 114, 300, 300);
		}
		return lblPicture;
	}
	JScrollPane getScpTypes() {
		if (scpTypes == null) {
			scpTypes = new JScrollPane();
			scpTypes.setBounds(21, 384, 476, 224);
			scpTypes.setViewportView(getTblTypes());
		}
		return scpTypes;
	}
	JTable getTblTypes() {
		if (tblTypes == null) {
			tblTypes = new JTable();
			tblTypes.setEnabled(false);
			tblTypes.setFont(new Font("David", Font.BOLD, 11));
			tblTypes.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null},
				},
				new String[] {
					"Category", "Type"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
			});
		}
		return tblTypes;
	}
	private JButton getBtnGoBackEP() {
		if (btnGoBackEP == null) {
			btnGoBackEP = new JButton("Back to Employee Portal");
			btnGoBackEP.addActionListener(new BtnGoBackEPActionListener());
			btnGoBackEP.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBackEP.setBounds(375, 642, 214, 66);
		}
		return btnGoBackEP;
	}
	private JLabel getLblProductId() {
		if (lblProductId == null) {
			lblProductId = new JLabel("Product ID:");
			lblProductId.setFont(new Font("David", Font.BOLD, 20));
			lblProductId.setBounds(21, 53, 164, 29);
		}
		return lblProductId;
	}
	private JTextField getTextProductID() {
		if (textProductID == null) {
			textProductID = new JTextField();
			textProductID.setEnabled(false);
			textProductID.setEditable(false);
			textProductID.setColumns(10);
			textProductID.setBackground(SystemColor.menu);
			textProductID.setBounds(246, 58, 251, 20);
		}
		return textProductID;
	}
	private JButton getBtnBackToSearch() {
		if (btnBackToSearch == null) {
			btnBackToSearch = new JButton("Back to Search Product");
			btnBackToSearch.addActionListener(new BtnBackToSearchActionListener());
			btnBackToSearch.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnBackToSearch.setBounds(21, 642, 214, 66);
		}
		return btnBackToSearch;
	}
	private JButton getBtnBackToTemplate() {
		if (btnBackToTemplate == null) {
			btnBackToTemplate = new JButton("Back to Templates Screen");
			btnBackToTemplate.addActionListener(new BtnBackToTemplateActionListener());
			btnBackToTemplate.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnBackToTemplate.setBounds(700, 642, 214, 66);
		}
		return btnBackToTemplate;
	}
	/**
	 * This inner class implements event listener for pressing Back to Search Product button
	 * @author Ori Ziv
	 *
	 */
	private class BtnBackToSearchActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new ProductSearchScreen());
		}
	}
	/**
	 * This inner class implements event listener for pressing Back to Employee Portal button
	 * @author Ori Ziv
	 *
	 */
	private class BtnGoBackEPActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new EmployeePortal());
		}
	}
	/**
	 * This inner class implements event listener for pressing Back to Templates Screen button
	 * @author Ori Ziv
	 *
	 */
	private class BtnBackToTemplateActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new TemplateManagementScreen());
		}
	}
}
